<?php

/**
 * This is the model class for table "{{subscription}}".
 *
 * The followings are the available columns in table '{{subscription}}':
 * @property integer $id
 * @property string $email
 * @property integer $status
 * @property string $create_time
 * @property string $update_time
 * @property string $ip_subscription
 * @property string $ip_unsubscribe
 * @property string $validation_code
 * @property string $subscription_time
 * @property string $unsubscribe_time
 */
class Subscription extends CActiveRecord
{

    /**
     * @return string the associated database table name
     */
    public function tableName()
    {
        return '{{subscription}}';
    }

    public function behaviors()
    {
        return array(
            'CTimestampBehavior' => array(
                'class'           => 'zii.behaviors.CTimestampBehavior',
                'createAttribute' => 'create_time',
                'updateAttribute' => 'update_time',
            )
        );
    }

    protected function beforeSave()
    {
        if ($this->isNewRecord)
            $this->ip_subscription = Yii::app()->request->userHostAddress;
        return parent::beforeSave();
    }

    /**
     * @return array validation rules for model attributes.
     */
    public function rules()
    {
        // NOTE: you should only define rules for those attributes that
        // will receive user inputs.
        return array(
            array('email', 'required'),
            array('email', 'email'),
            array('email', 'unique'),
            array('status', 'numerical', 'integerOnly' => true),
            array('email, validation_code', 'length', 'max' => 255),
            array('ip_subscription, ip_unsubscribe', 'length', 'max' => 25),
            array('create_time, update_time, subscription_time, unsubscribe_time', 'safe'),
            // The following rule is used by search().
            // @todo Please remove those attributes that should not be searched.
            array('id, email, status, create_time, update_time, ip_subscription, ip_unsubscribe, validation_code, subscription_time, unsubscribe_time', 'safe', 'on' => 'search'),
        );
    }

    /**
     * @return array relational rules.
     */
    public function relations()
    {
        // NOTE: you may need to adjust the relation name and the related
        // class name for the relations automatically generated below.
        return array(
        );
    }

    /**
     * @return array customized attribute labels (name=>label)
     */
    public function attributeLabels()
    {
        return array(
            'id'                => 'ID',
            'email'             => Yii::t('main', 'Email'),
            'status'            => Yii::t('main', 'Status'),
            'create_time'       => Yii::t('main', 'Subscription Time'),
            'update_time'       => Yii::t('main', 'Update Time'),
            'ip_subscription'   => Yii::t('main', 'IP Subscription'),
            'ip_unsubscribe'    => Yii::t('main', 'IP Unsubscribe'),
            'validation_code'   => Yii::t('main', 'Validation Code'),
            'subscription_time' => Yii::t('main', 'Subscription Time'),
            'unsubscribe_time'  => Yii::t('main', 'Unsubscribe Time'),
        );
    }

    /**
     * Retrieves a list of models based on the current search/filter conditions.
     *
     * Typical usecase:
     * - Initialize the model fields with values from filter form.
     * - Execute this method to get CActiveDataProvider instance which will filter
     * models according to data in model fields.
     * - Pass data provider to CGridView, CListView or any similar widget.
     *
     * @return CActiveDataProvider the data provider that can return the models
     * based on the search/filter conditions.
     */
    public function search()
    {
        // @todo Please modify the following code to remove attributes that should not be searched.

        $criteria = new CDbCriteria;

        $criteria->compare('id', $this->id);
        if (isset($_GET['subs_email']) AND $_GET['subs_email'] != '')
        {

            $criteria->compare('email', $_GET['subs_email'], true, 'AND', true);
        }
        //$criteria->compare('email',$this->email,true);
        $criteria->compare('status', $this->status);
        $criteria->compare('create_time', $this->create_time, true);
        $criteria->compare('update_time', $this->update_time, true);
        $criteria->compare('ip_subscription', $this->ip_subscription, true);
        $criteria->compare('ip_unsubscribe', $this->ip_unsubscribe, true);
        $criteria->compare('validation_code', $this->validation_code, true);
        $criteria->compare('subscription_time', $this->subscription_time, true);
        $criteria->compare('unsubscribe_time', $this->unsubscribe_time, true);

        return new CActiveDataProvider($this, array(
            'criteria' => $criteria,
            'sort'     => array(
                'defaultOrder' => array(
                    'id' => CSort::SORT_DESC,
                )
            )
        ));
    }

    /**
     * Returns the static model of the specified AR class.
     * Please note that you should have this exact method in all your CActiveRecord descendants!
     * @param string $className active record class name.
     * @return Subscription the static model class
     */
    public static function model($className = __CLASS__)
    {
        return parent::model($className);
    }

}
